package com.cos.emp.web.controller;

import com.cos.emp.util.SendMsgUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author: COS
 * @time: 2022/9/29 15:43
 * @description:
 */
@Controller
@RequestMapping("/sys")
public class SystemController {

    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/9/29 15:54
     * @description:
     * 这个方法仅操作发送手机短信和保存对应验证码,
     * 不需要他给前任何响应,所以是void
     */
    @RequestMapping(value = "/sendMsg/{phone}",method = RequestMethod.POST)
    public void sendMsm(@PathVariable("phone") String phone, HttpSession session){
        // 发送手机短信 并 获取验证码
        String code = SendMsgUtil.sendMsg(phone);
        session.setMaxInactiveInterval(60);
        session.setAttribute("phoneCode", code);
    }

    @RequestMapping(value = "/checkCode",method = RequestMethod.POST)
    @ResponseBody
    public String checkCode(String code,HttpSession session){
        String phoneCode = (String) session.getAttribute("phoneCode");
        System.out.println(phoneCode);
        if (phoneCode.equals(code)) {
            //验证码验证成功
            return "1";
        }
        //验证码验证失败
        return "0";
    }
}
